
package com.yllt4cloud.ddauth.service.impl;


import com.yllt4cloud.common.ddbase.constant.GlobalConstants;
import com.yllt4cloud.common.ddbase.domain.ClerkConfirmAuthority;
import com.yllt4cloud.common.ddbase.domain.ConfirmAuthority;
import com.yllt4cloud.ddauth.mapper.ClerkConfirmAuthorityMapper;
import com.yllt4cloud.ddauth.mapper.ConfirmAuthorityMapper;
import com.yllt4cloud.ddauth.service.IClerkConfirmAuthorityService;
import com.yllt4cloud.ddauth.service.base.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * <br>
 * <b>功能：</b>ClerkConfirmAuthorityServiceImpl<br>
 * <b>作者：</b>Administrator<br>
 * <b>日期：</b> Wed Feb 13 19:03:51 CST 2019 <br>
 * <b>版权所有： 2016,yllt.com<br>
 */
@Service
@Transactional
public class ClerkConfirmAuthorityServiceImpl extends BaseService<ClerkConfirmAuthority> implements IClerkConfirmAuthorityService {
	private final static Logger log= LoggerFactory.getLogger(ClerkConfirmAuthorityServiceImpl.class);

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private ClerkConfirmAuthorityMapper mapper;

	@Autowired
	private ConfirmAuthorityMapper confirmAuthorityMapper;

	public ClerkConfirmAuthorityMapper getMapper() {
		return mapper;
	}

	/*@Autowired
	private RedisTemplate redisTemplate;*/

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

	@Override
	public Integer insert(ClerkConfirmAuthority record) {
		mapper.insert(record);
		return record.getId();
	}


	/**
	 * 验证员工的权限
	 * @param clerkId
	 * @param url
	 * @return
	 */
	public Integer validateClerkAuthority(Integer clerkId,String url){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("clerkId",clerkId);
		paramMap.put("url",url);

		Integer allNum=mapper.countByParam(paramMap);
		if(allNum>0){
			return GlobalConstants.TRUE;
		}else{
			return GlobalConstants.FALSE;
		}
	}


	//验证 URL  是否在权限列表中，如果没有，则是不做权限设置
	public boolean  validateUrlQuanXian(Integer clerkId,String url){
		//获取场所代码
/*		String store_code= SystemConfigContainer.instance().getCsCode();
		ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
		String redis_key=store_code+RedisConstants.all_confirmAuthor_prefix;
		String key=operations.get(redis_key);
		if(key==null){  //获取所有的确认权限
			Map<String, Object> paramMap=new HashMap<String, Object>();
			List<ConfirmAuthority> list=confirmAuthorityMapper.query(paramMap);
			UserRoleContainer.instance().setUrlConfirmAuthorityMap(ListChangeUtil.list2ConcurrentMap(list, "", "url"));
            operations.set(redis_key,redis_key);
		}
		ConcurrentHashMap<String, ConfirmAuthority> urlConfirmAuthorityMap=UserRoleContainer.instance().getUrlConfirmAuthorityMap();
		if(urlConfirmAuthorityMap==null || urlConfirmAuthorityMap.size()==0){  //由于系统重启，导致urlConfirmAuthorityMap 数据为空，但是redis 却还有数据，即 key 不为空情况下
			Map<String, Object> paramMap=new HashMap<String, Object>();
			List<ConfirmAuthority> list=confirmAuthorityMapper.query(paramMap);
			UserRoleContainer.instance().setUrlConfirmAuthorityMap(ListChangeUtil.list2ConcurrentMap(list, "", "url"));
			urlConfirmAuthorityMap=UserRoleContainer.instance().getUrlConfirmAuthorityMap();

		}*/
		Map<String, Object> paramMap=new HashMap<String, Object>();
		List<ConfirmAuthority> list=confirmAuthorityMapper.query(paramMap);
		ConcurrentHashMap<String, ConfirmAuthority> urlConfirmAuthorityMap=null;



		//判断是否配置了该URL 的确认权限
		ConfirmAuthority vo=urlConfirmAuthorityMap.get(url);
		if(vo==null){   //不存在则默认不拦截
			return true;
		}
		//判断员工是否有该权限
		Integer flg=validateClerkAuthority(clerkId,url);
		if(flg.equals(GlobalConstants.TRUE)){
			return true;
		}else{   //没有权限，需要拦截
			return false;
		}

	}

	@Override
	public boolean checkNotInTabAuthoritymodeUrl(Integer clerkId, String url) {
		return false;
	}

	/**
     * 验证 当前请求的url  在 base_confirmauthority 中，却不在 base_authoritymode 中的权限，员工是否有权限
     * @param clerkId
     * @param url
     * @return
     */
	/*public boolean  checkNotInTabAuthoritymodeUrl(Integer clerkId,String url){

        //获取场所代码
        String store_code= SystemConfigContainer.instance().getCsCode();
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String redis_key=store_code+RedisConstants.not_in_authoritymode_prefix;
        Object key=operations.get(redis_key);
        if(key==null){  //获取所有在表base_confirmauthority 中，却不在base_authoritymode 中的权限
            Map<String, Object> paramMap=new HashMap<String, Object>();
            paramMap.put("notInTabAuthoritymode",1);
            List<ConfirmAuthority> list=confirmAuthorityMapper.queryNew(paramMap);
            //设置 不在表 base_authoritymode 中，在base_confirmauthority 表中的 确认权限信息 map
            UserRoleContainer.instance().setUrlNotInAuthoritymodeMap(ListChangeUtil.list2ConcurrentMap(list, "", "url"));
            operations.set(redis_key,redis_key);
        }
        ConcurrentHashMap<String, ConfirmAuthority> urlNotInAuthoritymodeMap=UserRoleContainer.instance().getUrlNotInAuthoritymodeMap();
        if(urlNotInAuthoritymodeMap==null || urlNotInAuthoritymodeMap.size()==0){  //由于系统重启，urlNotInAuthoritymodeMap 数据为空，但是redis 却还有数据，即 key 不为空情况下
            Map<String, Object> paramMap=new HashMap<String, Object>();
            paramMap.put("notInTabAuthoritymode",1);
            List<ConfirmAuthority> list=confirmAuthorityMapper.queryNew(paramMap);
            UserRoleContainer.instance().setUrlNotInAuthoritymodeMap(ListChangeUtil.list2ConcurrentMap(list, "", "url"));
            urlNotInAuthoritymodeMap=UserRoleContainer.instance().getUrlNotInAuthoritymodeMap();

        }
        //判断是否配置了该URL 的确认权限
        ConfirmAuthority vo=urlNotInAuthoritymodeMap.get(url);
        if(vo==null){   //不存在则默认不拦截
            return true;
        }
        //判断员工是否有该权限
        Integer flg=validateClerkAuthority(clerkId,url);
        if(flg.equals(GlobalConstants.TRUE)){
            return true;
        }else{   //没有权限，需要拦截
            return false;
        }
    }*/
}
